package com.sz.stock.service;

import com.sz.stock.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CardApiServiceImpl implements ICardApiService {

    @Override
    //身份证实名绑定认证；
    public String checkCard(String name, String cardNum, String id) {       //, String phone
        String host = "https://lundroid.market.alicloudapi.com";
        String path = "/lianzhuo/verifi";
        String method = "GET";
        String appcode = "97edf69b93d3405ea9ceb07964070cb8";  //不要测试啊，次数有限，下面有成功和失败的例子！！！
        Map<String, String> headers = new HashMap<>();
//      最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<>();
        querys.put("acct_name", name);
        querys.put("acct_pan", cardNum);    //银行卡号
        querys.put("cert_id", id);          //身份证
//      querys.put("phone_num", phone);     //手机号码

        String result = "";
        try {
            /*
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */

            //运行以下代码是要收费的
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            // 获取response的body
            result = EntityUtils.toString(response.getEntity());

            /*
             * 返回的 body 数据:
             * {"data":{"bank_url":"http:\/\/www.citicbank.com","bank_name":"中信银行","bank_id":"03020000","card_name":"借记卡","card_type":"借记卡","bank_phone":"95558","bank_logo":"http:\/\/img.hotp.cn\/6cd57e.png"},"resp":{"code":0,"desc":"OK"}}
             * {"data":{"bank_url":"http:\/\/www.abchina.com","bank_name":"农业银行","bank_id":"01030000","card_name":"金穗海通卡","card_type":"借记卡","bank_phone":"95599","bank_logo":"http:\/\/img.hotp.cn\/7a898a.png"},"resp":{"code":5,"desc":"持卡人认证失败"}}
             * */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
